package com.business.user.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信工具类
 */
public class SmsUtil {

	private static String SIGN_NAME ;//签名

	// 产品名称:云通信短信API产品,开发者无需替换
	private static final String product = "Dysmsapi";
	// 产品域名,开发者无需替换
	private static final String DOMAIN = "dysmsapi.aliyuncs.com";

	private static  String ACCESS_KEY_ID = null;
	private static  String ACCESS_KEY_SECRET = null;

	private static Logger logger = LoggerFactory.getLogger(SmsUtil.class);

	public SmsUtil(){

	}

	public static void setSignName(String signName){
		SmsUtil.SIGN_NAME = signName;
	}
	public static void setAccessKeyId(String accessKeyId){
		SmsUtil.ACCESS_KEY_ID = accessKeyId;
	}
	public static void setAccessKeySecret(String accessKeySecret){
		SmsUtil.ACCESS_KEY_SECRET = accessKeySecret;
	}

	private static IAcsClient getClient(){
		DefaultProfile profile = DefaultProfile.getProfile("default", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
		IAcsClient client = new DefaultAcsClient(profile);
		return  client;
	}

	private static CommonRequest getRequest(String phone, String templateCode){

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain(DOMAIN);
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("TemplateCode", templateCode);
		request.putQueryParameter("SignName", SIGN_NAME);
		request.setProduct(product);
		return  request;
	}

	/**
	 * 发送验证码
	 * @param phone
	 * @param validateCode
	 * @param templateCode
	 * @return
	 */
	public static  boolean sendSms(String phone, String validateCode,String templateCode){
		Map<String,String> map = new HashMap<>();
		map.put("code",validateCode);
		return  sendSms(phone,map,templateCode);
	}

	/**
	 * 发送通知短信
	 * @param phone
	 * @param templateParam
	 * @param templateCode
	 * @return
	 */
	public static  boolean sendSms(String phone, Map<String,String > templateParam, String templateCode){
		CommonRequest request = getRequest(phone,templateCode);
		request.putQueryParameter("TemplateParam", JSONObject.toJSONString(templateParam));
		try {
			CommonResponse response = getClient().getCommonResponse(request);
			JSONObject jsonObject = JSONObject.parseObject(response.getData());
			if("OK".equals(jsonObject.getString("Code"))){
				return true;
			}else{
				logger.error(jsonObject.toJSONString());
			}
		} catch (ServerException e) {
			logger.error(e.getMessage(),e);
		} catch (ClientException e) {
			logger.error(e.getMessage(),e);
		}
		return false;
	}

}
